package com.blockchainenergy.blockChain;

import com.blockchainenergy.common.CarbonTools;
import com.blockchainenergy.common.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;

/**
 * @descriptions: 测试API用
 * @data: 2021/11/4 21:11
 */
@RestController //本注解说明本类对象为spring容器管理的对象，并且说明这是一个控制器组件
@RequestMapping("/blockchain")//本注解说明本控制器所有路径都以。。。开头
public class BlockChainAPI {
    @Resource
    private BlockChainService blockChainService;

    /**
     * 获得带Hash的block
     */
    @GetMapping("/getBlockWithHash")
    public Result getBlockWithHash() throws Exception {
        Map<String, Object> fullChainWithHash = blockChainService.getFullChainWithHash();
        return Result.success(fullChainWithHash);
    }

    /**
     * 获得最后十块带Hash的值
     */
    @GetMapping("/getLast10BlockWithHash")
    public Result getLast10BlockWithHash() throws Exception {
        Map<String, Object> last10BlockWithHash = blockChainService.getLast10BlockWithHash();
        return Result.success(last10BlockWithHash);
    }


    @GetMapping("/chain")
    public Result fullChain() throws Exception {
//        return Result.success(blockChainService.getFullChain());
        return Result.success(blockChainService.getFullChainWithHash());
    }


    @GetMapping("/addBlock")
    public Result addBlock(@RequestParam Integer num) throws Exception {
        while (num-- > 0) CarbonTools.add(0, 0, 0, 0, 0);
        return Result.success(DataChain.getInstance().getChain());
    }

    @GetMapping("/getBlockByIndex")
    public Result getBlockByIndex(@RequestParam Integer index) throws Exception {
        return Result.success(blockChainService.getBlockByIndex(index));
    }

    @PostMapping("/matrixToBlock")
    public Result matrixToBlock(@RequestBody int[][] matrix) throws Exception {
        blockChainService.matrixToBlock(matrix);
        return Result.success(DataChain.getInstance().getChain());
    }

    @GetMapping("/getCarbonFlowRecord")
    public Result getCarbonFlowRecord(@RequestParam Integer node_number) throws Exception {
        return Result.success(CarbonTools.get_carbon_flow_record(node_number));
    }

    @GetMapping("/getCarbonPotentialRecord")
    public Result getCarbonPotentialRecord(@RequestParam Integer node_number) throws Exception {
        return Result.success(CarbonTools.get_carbon_potential_record(node_number));
    }
}
